// 订单详情
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:{},
    //pay=true 支付时不能点击按钮
    pay:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var order_id = wx.getStorage({
      key: 'order_id',
      success: function(res) {
        var order_id = res.data;
        wx.request({
          url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/orderDetail',
          data:{
            order_id:order_id
          },
          header:{
            "content-type":"application/x-www-form-urlencoded"
          },
          method:"POST",
          success:function(res){
            var order = res.data.data
            this.setData({
              order:order
            })
          }.bind(this),
          fail:function(res){
            wx.showModal({
              title: '提示',
              content: res.data.message,
              showCancel:false
            })
          }
        })
      }.bind(this),
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.name,
      path: "/pages/shouye/shouye",
      imageUrl: app.globalData.headPic
    }
  },
  /**
   * 删除订单
   */
  remove:function(){
    var order_id = this.data.order.order_id;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/delOrder',
      data:{
        order_id:order_id
      },
      header:{
        "content-type":"application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(res){
        wx.showToast({
          title: res.data.message,
          success:function(){
            setTimeout(function(){
              wx.navigateTo({
                url: '/mine/myOrder/myOrder',
              })
            },500)
            
          }
        })
      
      }
    })
  },
  /**
   * 确认收货
   */
  submit:function(){
    var order = this.data.order;
    var order_id = order.order_id;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/makeSure',
      data:{
        order_id:order_id
      },
      header:{
        "content-type":"application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(res){
        wx.request({
          url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/orderDetail',
          data: {
            order_id: order_id
          },
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          method: "POST",
          success: function (res) {
            var order = res.data.data
            this.setData({
              order: order
            })
          }.bind(this),
          fail: function (res) {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              showCancel: false
            })
          }
        })
      }.bind(this),
      fail:function(res){
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel:false
        })
      }
    })
  },
  /**
   * 付款
   */
  payment: function () {
    this.setData({
      pay:true
    })
    var order = this.data.order;
    var shop_id = order.shop_id
    var m_id = app.globalData.user.m_id;
    var order_sn = order.order_sn;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/Pay/wxPay',
      data: {
        shop_id: shop_id,
        m_id: m_id,
        order_sn: order_sn
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (res) {
        if (res.data.data == "") {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel: false
          })
          this.setData({
            pay: false
          })
        }
        var data = JSON.parse(res.data).pay_message
        var message = JSON.parse(data);
        var timeStamp = message['timeStamp'];
        var nonceStr = message['nonceStr'];
        var packaged = message['package'];
        var signType = message['signType'];
        var paySign = message['paySign'];
        wx.requestPayment({
          timeStamp: timeStamp,
          nonceStr: nonceStr,
          package: packaged,
          signType: signType,
          paySign: paySign,
          success: function (res) {
            //  2 待发货
            this.setData({
              pay: false
            })
            wx.setStorage({
              key: 'order',
              data: '2',
              success: function () {
                wx.navigateTo({
                  url: '/mine/myOrder/myOrder',
                })
              }
            })
          }.bind(this),
          fail: function (res) {
            //  1 待付款
            this.setData({
              pay: false
            })
            if (res.errMsg == "requestPayment:fail cancel") {
            
            } else {
              wx.showModal({
                title: '提示',
                content: res.errMsg,
                showCancel: false
              })
            }
          }.bind(this)
        })
      }.bind(this),
      fail: function (res) {
        wx.showModal({
          title: '提示',
          content: res.data.message,
        })
        this.setData({
          pay: false
        })
      }.bind(this)
    })
  },
  /**
   * 评价
   */
  assess:function(){
      var order = this.data.order;
      var order_id = order.order_id;
      wx.setStorage({
        key: 'order_id',
        data: order_id,
        success:function(){
          wx.navigateTo({
            url: '/order/assess/assess',
          })
        }
      })
  }
})